package com.liu.sportplat.controller;

import com.alibaba.fastjson.JSON;
import com.liu.sportplat.bean.User;
import com.liu.sportplat.dao.UserMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    UserMapping userMapping;

    @CrossOrigin
    @RequestMapping("/login")
    public String login(@RequestBody User user) {
        String flag = "error";
        //通过查找数据库中是否有该用户来得到返回值，如果没有则返回空
        User userByMessage = userMapping.getUserByMessage(user.getUsername(), user.getPassword());
        HashMap<String, Object> res = new HashMap<>();
        if (userByMessage != null) {
            flag = "ok";
        }
        res.put("flag", flag);
        res.put("user", userByMessage);
        String res_json = JSON.toJSONString(res);
        return res_json;
    }
}
